using VSTalk.Engine.Core.Notifications.SystemMessages;
using VSTalk.Engine.Core.XmppConnection;

using agsXMPP;
using agsXMPP.protocol.client;

using VSTalk.Engine.Core.Notifications;

namespace VSTalk.Engine.Core.XmppCommunication.Services.Authorization
{
    public class AuthorizationProvider : BaseCommunicationService, IAuthorizationService
    {
        private INotificationQueue Queue
        {
            get { return AppServices.Get<INotificationQueue>(); }
        }

        private PresenceManager _presenceManager;

        public override void Initialize(IClientConnection connection)
        {
            base.Initialize(connection);
            _presenceManager = new PresenceManager(Connection);
        }

        public void SendAuthorizationRequest(Jid to)
        {
            _presenceManager.Subscribe(to);
        }

        public void HandleAuthorizationRequest(Jid @from)
        {
            Queue.PushToFront(new AuthorizationRequestMessage(
                    @from,
                    () => _presenceManager.ApproveSubscriptionRequest(@from),
                    () => _presenceManager.RefuseSubscriptionRequest(@from)));
        }
    }
}